package com.caishi.lkx.exam.vo;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.caishi.lkx.exam.dto.QuestionSimpleDto;
import com.caishi.lkx.exam.es.model.QuestionEs;
import com.caishi.lkx.exam.ienum.type.QuestionDifficulty;
import com.caishi.lkx.exam.ienum.type.QuestionType;
import com.caishi.lkx.exam.model.QuestionModel;
import com.caishi.lkx.exam.question.QuestionDataProcessComposite;
import com.caishi.lkx.exam.records.dto.QuestionRecordsDtoItem;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Map;

@Schema(title = "客户端试题实体类（答案加密）")
@Setter
@Getter
public class QuestionVo {

    protected QuestionVo() {}

    @Schema(title = "id")
    private String id;

    @Schema(title = "题型")
    private QuestionType firstType;

    @Schema(title = "小题型")
    private String type;

    @Schema(title = "难易度")
    private QuestionDifficulty difficulty;

    @Schema(title = "题干")
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<Map<String, Object>> stem;

    @Schema(title = "选项")
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<Map<String, Object>> quOption;

    @Schema(title = "答案解析")
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<Map<String, Object>> answerDesc;

    @Schema(hidden = true)
    private List<QuestionVo> children;

    @Schema(title = "做题数据")
    private QuestionRecordsDtoItem record;

    @Schema(title = "正确答案")
    private Object rightAnswer;

    @Schema(title = "正确答案")
    private String answerBase64;

    @Schema(title = "试题签名")
    private String sign;

    @Schema(title = "是否收藏")
    private Boolean collect;

    @Schema(title = "所属集合id")
    private String cid;

    @Schema(title = "所属集合名称")
    private String cn;

    @Schema(title = "试题源id")
    private String sourceId;

    @Schema(title = "做题次数")
    private Integer doingNumber;

    @Schema(title = "错误次数")
    private Integer errorNumber;

    public static QuestionVo simpleDtoTrans(QuestionSimpleDto simpleDto) {
        QuestionVo vo = new QuestionVo();
        BeanUtil.copyProperties(simpleDto, vo);
        vo.setChildren(simpleDto.getChildren() == null ? null : simpleDto.getChildren().stream().map(QuestionVo::simpleDtoTrans).toList());
        return vo;
    }

    public static QuestionVo modelTrans(QuestionModel model) {
        QuestionVo vo = new QuestionVo();
        BeanUtil.copyProperties(model, vo);
        vo.setRightAnswer(model.getAnswer());
        vo.setSign(QuestionDataProcessComposite.instance.questionContentSign(model));
        vo.setChildren(model.getChildren() == null ? null : model.getChildren().stream().map(QuestionVo::modelTrans).toList());
        return vo;
    }

    public static QuestionVo modelTransByEs(QuestionEs model) {
        QuestionVo vo = new QuestionVo();
        BeanUtil.copyProperties(model, vo);
        vo.setRightAnswer(model.getAnswer());
        return vo;
    }

}
